<?php
session_start();
if (isset($_POST['btnLog'])==true){	
	require_once("../dbcon.php");  
	$username = $_POST['username'];
	$password = md5($_POST['password']);
  	//$password = $_POST['password'];
	if (get_magic_quotes_gpc()== false) {
		$username=trim(mysql_real_escape_string($username));
		$password=trim(mysql_real_escape_string($password));
	}
	$sql = "SELECT * FROM users WHERE username='$username' AND password ='$password'";
	$user = mysql_query($sql);
	if (mysql_num_rows($user)==1) {//Thành công	
if (isset($_POST['nho'])== true){
	 setcookie("un", $_POST['username'], time() + 60*60*24*7 );
	 setcookie("pw", $_POST['password'], time() + 60*60*24*7 );
} else {
	 setcookie("un", $_POST['username'], time() -1);
	 setcookie("pw", $_POST['password'], time() -1);
}
$row_user = mysql_fetch_assoc($user);
$_SESSION['kt_login_id'] = $row_user['idUser'];
$_SESSION['kt_login_user'] = $row_user['Username'];
$_SESSION['kt_login_level'] = $row_user['idGroup'];
$_SESSION['kt_HoTen'] = $row_user['HoTen'];
$_SESSION['kt_GioiTinh'] = $row_user['GioiTinh'];

if (strlen($_SESSION['back'])>0){
	$back = $_SESSION['back']; unset($_SESSION['back']);
	header("location:$back");
} else header("location: index.php");
	} else { //Thất bại
    header("location: dangnhap.php");
  }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Care With Love Administrator</title>
<link href="../css/oh_admin.css" rel="stylesheet" type="text/css" />
<style>
#username, #password{
	background-color: #fdecf2;
	color: #C40067;
	padding: 3px;
	border: 1px solid #C40067;
	width: 150px;
	font-weight: bold;
	height: 13px;
}
#btnLog{
	background-color: #C40067;
	color: #FD6FBD;
	width: 120px;
	padding: 3px;
	border: 1px solid #FD6FBD
}
.style2 {font-size: 12px}
.style3 {
	color: #C40067
}
</style>
</head>

<body>
<div align="center">
  <div id="admin">
    <!-- HIDE DIV HEADER
    <div id="header">
      <div id="header_left"></div>
      <div id="header_right"></div>
      <div id="header_center"></div>
    </div>
    -->
    <div id="header"><img src="../images/logo_cwl.png" width="271" height="93" /></div>
    <div id="content">
    <!----------------------------------------- DIV LOGIN ----------------------------------------->
    <div id="login_dad">

        <div align="center" id="login">
          <div id="login_label">
            <p class="style3" id="text_label">Care With Love! Administrator login</p>
          </div>
          <div id="login_content">
            <div id="login_left">
              <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td id="text"><p>Nhập username và mật khẩu của Administrator để quản trị site CareWithLove.</p>
                  <p id="comeback"><a href="../index.php">Quay lại trang chủ</a></p></td>
                </tr>
                <tr>
                  <td><div align="center"><img src="../images/clock.gif" width="105" height="131" /></div></td>
                </tr>
              </table>
              <p>&nbsp;</p>
            </div>
            <div id="login_right">
              <div id="left_boder">
                <div id="left_1"></div>
                <div id="left_2"></div>
                <div id="left3"></div>
              </div>
              <div id="right_boder">
                <div id="right_1"></div>
                <div id="right_2"></div>
                <div id="right_3"></div>
              </div>
              <div id="center_boder">
                <div id="center_1"> </div>
                <!--------------------------------------- FROM LOGIN --------------------------------------->
                <div id="center_2">
                  <form id="form1" name="form1" method="post" action="">
                    <table id=tbldn width=276 border=0 align=center cellpadding=4 cellspacing=0>
                      <tr>
                        <td id="text">Username</td>
                        <td><input name="username" type="text" id="username"/></td>
                      </tr>
                      <tr>
                        <td id="text">Password</td>
                        <td><input name="password" type="password" id="password" /></td>
                      </tr>
                      <tr>
                        <td>&nbsp;</td>
                        <td><input type="submit" name="btnLog" id="btnLog" value="Đăng nhập"/>
                        </td>
                      </tr>
                    </table>
                  <div align="center" id="error1"> 
      	                      <?php 
							  	echo $_SESSION['error']; unset( $_SESSION['error'] );
						  		?>
                  </div>
                  </form>
                </div>
                <!--------------------------------------- FROM LOGIN --------------------------------------->
                <div id="center_3"></div>
              </div>
            </div>
          </div>
        </div>

    </div>
    <!----------------------------------------- DIV LOGIN ----------------------------------------->
    </div>
    
    <div id="footer"><span id="result_box" lang="en" xml:lang="en">C<span title="Click for alternate translations">opyright</span></span> © 2012 - <span class="bkvtin"><a href="ymsgr:sendim?bkvtin&amp;m=Hello BKVTIN">Trần Hữu Trung Tín</a></span></div>
  </div>
</div>
</body>
</html>
